/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.dummydevice;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import net.zarubsys.robotplayground.core.Environment;

import org.junit.Test;

/**
 * DummyDeviceTest
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyDeviceTest {

	private static final int POSITION_TEST = 10;
	private static final int DIRECTION_INIT_TEST = 90;
	private static final int Y_INIT_TEST = 1;
	private static final int X_INIT_TEST = 1;

	@Test
	public void testInit() {
		DummyDevice device = initTestDevice();
		assertEquals(device.getX(), X_INIT_TEST);
		assertEquals(device.getY(), Y_INIT_TEST);
		assertEquals(device.getDirection(), DIRECTION_INIT_TEST);
	}

	@Test
	public void testX() {
		DummyDevice device = initTestDevice();
		device.lockMovementsAccess();
		device.setX(POSITION_TEST);
		assertEquals(device.getX(), POSITION_TEST);
		device.unlockMovementsAccess();
	}

	@Test
	public void testY() {
		DummyDevice device = initTestDevice();
		device.lockMovementsAccess();
		device.setY(POSITION_TEST);
		assertEquals(device.getY(), POSITION_TEST);
		device.unlockMovementsAccess();
	}

	private DummyDevice initTestDevice() {
		DummyDeviceConfiguration cfg = new DummyDeviceConfiguration(X_INIT_TEST, Y_INIT_TEST, DIRECTION_INIT_TEST);
		DummyDevice device = new DummyDevice();
		device.init(cfg, new Environment(""));
		return device;
	}

	@Test
	public void testDirection() {
		DummyDevice device = initTestDevice();
		device.lockMovementsAccess();
		device.setDirection(POSITION_TEST);
		assertEquals(device.getDirection(), POSITION_TEST);
		device.unlockMovementsAccess();
	}

	@Test(expected = IllegalStateException.class)
	public void testLockMovementsAccessX() {
		DummyDevice device = initTestDevice();
		device.setX(POSITION_TEST);
	}
	
	@Test(expected = IllegalStateException.class)
	public void testLockMovementsAccessY() {
		DummyDevice device = initTestDevice();
		device.setY(POSITION_TEST);
	}
	
	@Test(expected = IllegalStateException.class)
	public void testLockMovementsAccessDirection() {
		DummyDevice device = initTestDevice();
		device.setDirection(POSITION_TEST);
	}

	@Test(expected = IllegalMonitorStateException.class)
	public void testLocking() {
		DummyDevice device = initTestDevice();
		assertFalse(device.movementsLock.isLocked());
		device.lockMovementsAccess();
		assertTrue(device.movementsLock.isLocked());
		device.unlockMovementsAccess();
		assertFalse(device.movementsLock.isLocked());
		device.unlockMovementsAccess();
	}

}

